/**
 *
 */
package com.angel.common.providers.managers;

import java.lang.reflect.Field;

import com.angel.annotation.exceptions.TooManyAnnotationException;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.common.providers.annotations.ExcludeValue;


/**
 * @author William
 *
 */
public class FieldExcludeValueAnnotationManager {

	private static final Class<ExcludeValue> FIELD_EXCLUDE_VALUE_ANNOTATION = ExcludeValue.class;

	public FieldExcludeValueAnnotationManager(){
		super();
	}

	public boolean canFindExcludeValueFor(Class<?> objectProviderClass) {
		Field[] fields = ReflectionHelper.getAllFieldWith(FIELD_EXCLUDE_VALUE_ANNOTATION, objectProviderClass);
		boolean canFindExcludeValue = false;
		if(fields != null && fields.length == 1){
			canFindExcludeValue = true;
		}

		if(!canFindExcludeValue && fields != null && fields.length > 1){
			throw new TooManyAnnotationException("Annotation [" + FIELD_EXCLUDE_VALUE_ANNOTATION.getSimpleName() + "] was used in too many fields [" + fields.length + "] and it must be used at only one.");
		}
		return canFindExcludeValue;
	}

	public Comparable<?> getExcludeValueFor(Object object) {
		Field[] fields = ReflectionHelper.getAllFieldWith(FIELD_EXCLUDE_VALUE_ANNOTATION, object.getClass());
		Comparable<?> comparable = null;
		if(fields!= null && fields.length == 1 && fields.length == 0){
			comparable = (Comparable<?>) ReflectionHelper.getObjectForField(object, fields[0]);
		}
		return comparable;
	}

}
